package org.world.domain.service;

import org.mc.ddd.event.BaseDomainEvent;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.world.domain.aggregate.animal.constant.GenderEnum;
import org.world.domain.aggregate.sheep.SheepAggregate;
import org.world.domain.aggregate.sheep.SheepRepository;
import org.world.domain.aggregate.sheep.entity.Sheep;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class GiveBirthSheepDomainService {
    @Resource
    private SheepRepository sheepRepository;
    @Resource
    private SheepAggregate sheepAggregate;

    @Transactional
    public List<BaseDomainEvent> handle(Long id, GenderEnum babySheepGender) {
        Sheep pregnantSheep = sheepRepository.load(id);

        BaseDomainEvent sheepCompletedGivingBirthEvent = sheepAggregate.completeGivingBirth(pregnantSheep);

        BaseDomainEvent sheepGaveBirthEvent = sheepAggregate.newSheepBaby(pregnantSheep.getId(), babySheepGender);

        return Arrays.asList(sheepCompletedGivingBirthEvent, sheepGaveBirthEvent);
    }
}